//
//  NetworkTools.swift
//  AFNetWorking - 封装
//
//  Created by wukeng on 16/8/24.
//  Copyright © 2016年 wukeng. All rights reserved.
//

import AFNetworking
/*
 1> 通过shareInstance拿到的永远是一个实例
 2> 不管三七二十一，永远只有一个实例
 */
enum RequestType : String{
    case GET = "GET"
    case POST = "POST"
}

class NetworkTools: AFHTTPSessionManager {
    // let是线程安全的 单例
    static let shareInstance : NetworkTools = {
       let tools = NetworkTools()
        tools.responseSerializer.acceptableContentTypes?.insert("text/html")
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        
        return tools
    }()
}

//封装请求方法
extension NetworkTools{
    func request(methodType : RequestType,urlString : String,paramerters : [String : AnyObject],finish : (result : AnyObject?,error : NSError?) -> ()) {
        let success = { (task : NSURLSessionDataTask, result : AnyObject?) in
           finish(result: result, error: nil)
        }
        let fail = { (tast : NSURLSessionDataTask?, error : NSError) in
           finish(result: nil, error: error)
        }
    
        switch methodType {
        case .GET:
            GET(urlString, parameters: paramerters, success: success, failure: fail)
        case .POST:
            POST(urlString, parameters: paramerters, success: success, failure: fail)
        }
    }
}

// MARK: -Access_token网络请求
extension NetworkTools{
    func loadAccess_Token(code : String,finish : (result :  [String : AnyObject]?, error : NSError?) -> ()) {
        //1.获取请求的URLSting
        let urlString = "https://api.weibo.com/oauth2/access_token"
        
        //2.请求的参数
        let parameter = ["client_id" : app_Key,"client_secret" : app_secret,"grant_type" : "authorization_code","code" : code,"redirect_uri" : redirect_uri]
        
        //3.发送网络请求
        request(.POST, urlString: urlString, paramerters: parameter) { (result, error) in
            finish(result: result as? [String : AnyObject], error: error)
        }
    }
}

// MARK: - 请求登录用户信息
extension NetworkTools{
    func loadUserinfo(access_token : String, uid : String,finish : (result : [String : AnyObject]?,error : NSError?) -> ()) {
        //1.获取请求的url
        let urlString = "https://api.weibo.com/2/users/show.json"
        //2.请求的参数
        let parameter = ["access_token" : access_token,"uid" : uid]
        //3.发送网络请求
        request(.GET, urlString: urlString, paramerters: parameter) { (result, error) in
            finish(result: result as? [String : AnyObject], error: error)
            
        }
    }
}

// MARK: - 请求首页数据
extension NetworkTools{
    func loadStatuses(finish : (result : [[String : AnyObject]]?,error : NSError?) -> ()) {
        //1.获取请求的url
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        //2.获取请求参数
        let parameter = ["access_token" : (UserAccountViewModel.single.account?.access_token)!]
        //3.发送网络请求
        request(.GET, urlString: urlString, paramerters: parameter) { (result, error) in
            
            guard let resultDict = result as? [String : AnyObject] else{
                finish(result: nil, error: error)
                return
            }
            finish(result: resultDict["statuses"] as? [[String : AnyObject]], error: error)
        }
    }
}




